<?php
namespace Admin\Model;
use Think\Model;
class RelativeModel extends Model {

	//已授权
	public function get_list_granted($page=1,$pagesize=10,$where_data="",$order="u.id desc"){
      
        $where="";
		foreach ($where_data as $key => $value) {
			 $where.=" and  u.".$key." like '%".$value."%'";
		}
		//已授权未过期
		$now_time=time();
		$where_str="(u.endtime>'$now_time' or u.endtime=0) ";


        $where_str.=" and r.rv_state=0 ".$where;

		
		$info=$this->alias('r')->join(' tp_user as u  ON r.user_id = u.id')->where($where_str)->order($order)->page($page.','.$pagesize)->select();

		// print_r($this->_sql());exit();
		foreach ($info as $k => $v) {
			if($v['endtime']==0){
				$info[$k]['endtime']="永久";
			}else{
				$info[$k]['endtime']=date("Y-m-d",$v['endtime']);
			}
		}
		$count=$this->alias('r')->join(' tp_user as u  ON r.user_id = u.id')->where($where_str)->count();


		$Page=new\Think\Page($count,$pagesize);	

		//分页跳转的时候保证查询条件
		foreach($where_data as $key=>$val) {
		    $Page->parameter[$key]   =   urlencode($val);
		}

		$show=$Page->show();
		return array("list"=>$info,"page"=>$show);
	}

	public function get_info1(){
		$phone=I('phone');
		$info=$this->join("tp_user ON tp_relative.user_id = tp_user.id ")->where("tp_relative.phone={$phone}")->order("tp_relative.id desc")->page($page.',10')->select();
		foreach ($info as $k => $v) {
			if($v['endtime']==0){
				$info[$k]['endtime']="永久";
			}else{
				$info[$k]['endtime']=date("Y-m-d",$v['endtime']);
			}
		}
		return array("list"=>$info);
	}

	public function sdelete(){
		$data['user_id']=I('user_id');
		$info=$this->where($data)->delete();

		$data_user['id']=I('user_id');
		$result=M('user')->where($data_user)->delete();
		return $result;
	}
	//未授权
	public function get_list($page=1,$pagesize=10,$where_data="",$order="u.id desc"){
        $where="";
		foreach ($where_data as $key => $value) {
			 $where.=" and  u.".$key." like '%".$value."%'";
		}

		//未授权已过期
		$now_time=time();
		$where_str="((u.endtime<'1460012896' and u.endtime!=0) or r.rv_state=1) ";

        $where_str.=$where;

		
		$info=$this->alias('r')->join(' tp_user as u  ON r.user_id = u.id')->where($where_str)->order($order)->page($page.','.$pagesize)->select();

		
		foreach ($info as $k => $v) {
			if($v['endtime']==0){
				$info[$k]['endtime']="永久";
			}else{
				$info[$k]['endtime']=date("Y-m-d",$v['endtime']);
			}
		}
		$count=$this->alias('r')->join(' tp_user as u  ON r.user_id = u.id')->where($where_str)->count();


		$Page=new\Think\Page($count,$pagesize);	

		//分页跳转的时候保证查询条件
		foreach($where_data as $key=>$val) {
		    $Page->parameter[$key]   =   urlencode($val);
		}

		$show=$Page->show();
		return array("list"=>$info,"page"=>$show);
	}

	public function shouquan(){
		$user_id=I('user_id');
		$info=$this->join("tp_user ON tp_relative.phone = tp_user.phone ")->where("tp_relative.user_id={$user_id}")->find();

		//过期的授权
		if($info['endtime'])
		{
			if($info['endtime']<time())
			{
			   $data_user['time']=3;
               $data_user['endtime']=$this->month_to_time(3);
               
               $result1=M('user')->where("id='$user_id'")->save($data_user);
			}
		}

// return $result;exit;
		$data['rv_state']=0;
		$result2=$this->where("user_id='$user_id'")->save($data);
		if($result1 || $result2){

			//日志记录
			$log_data['user_id']=$info['id'];
			$log_data['value']=$info['name']."获得授权,手机号码：".$info['phone'];
			$log_data['ctime']=time();
			M("user_log")->add($log_data);


			$code=$info['shouquan'];
			$this->mobile_code = $info['pw'];
			$this->name=$info['name'];
			$message = "【微百佳】".$this->name.",您已通过润晟堂经销商授权,授权号为".$code.",初始密码为". $this->mobile_code."请妥善保管" ;
			$sms_error = '';
			$mobile=$info['phone'];

			$target = "http://xtx.telhk.cn:8080/sms.aspx?action=send";
			$post_data =  "userid=6013&account=weibaijia&password=weiwei7788&mobile={$mobile}&content={$message}&sendTime=&extno=";
			$gets =  $this->xml_to_array(post_data($post_data, $target));
			if($gets['returnsms']['returnstatus']=="Success"){
				return "短信已发出，请注意查收！";
			}else{
				return "短信发送失败，请重发！";
			}
		}else{
			return "授权失败！";
		}
	}
	public function sqdelete(){
		$data['user_id']=I('user_id');
		$info=$this->where($data)->delete();

		$data_user['id']=I('user_id');
		$result=M('user')->where($data_user)->delete();
		if($result){
			$message = "【微百佳】您的授权申请审核未通过！";

			$sms_error = '';
			$mobile=$data['phone'];

			$target = "http://xtx.telhk.cn:8080/sms.aspx?action=send";
			$post_data =  "userid=6013&account=weibaijia&password=weiwei7788&mobile={$mobile}&content={$message}&sendTime=&extno=";
			$gets =  $this->xml_to_array(post_data($post_data, $target));
			if($gets['returnsms']['returnstatus']=="Success"){
				return "短信已发出，请注意查收！";
			}else{
				return "短信发送失败，请重发！";
			}
		}else{
			return "授权失败！";
		}
	}

	protected function xml_to_array($xml){
		$reg = "/<(\w+)[^>]*>([\\x00-\\xFF]*)<\\/\\1>/";
		if(preg_match_all($reg, $xml, $matches)){
			$count = count($matches[0]);
			for($i = 0; $i < $count; $i++){
				$subxml= $matches[2][$i];
				$key = $matches[1][$i];
				if(preg_match( $reg, $subxml )){
					$arr[$key] = $this->xml_to_array( $subxml );
				}else{
					$arr[$key] = $subxml;
				}
			}
		}
		return $arr;
	}

	//生成官方授权码
   public function build_offical()
   {
      $offical=65;
      $code=chr($offical);

      $info=1;

      while($info==1){
        $offical=$offical+1;
        if($offical>91){
           return false;
        }
        $code=chr($offical);
        $info=$this->where("shouquan='{$code}'")->count();
      }
      return $code;
   }
    //生成大区授权码
   public function build_zoon($offical)
   {
      $data2['offical']=$offical;
      $code = M('relative')->distinct ( true )->order('zoon desc')->where($data2)->getField ( 'zoon' );
      $code=$code+1;
      if($code>99){
         return false;
      }
      if(mb_strlen($code)<2){
        $code="0".$code;
      }

       return $code;
   }
     //生成总代理授权码
   public function build_agent($offical,$zoon)
   {
      $data2['offical']=$offical;
      $data2['zoon']=$zoon;
      $code = M('relative')->distinct ( true )->order('agent desc')->where($data2)->getField ( 'agent' );
      $code=$code+1;
      if($code>99){
        return false;
      }
      if(mb_strlen($code)<2){
        $code="0".$code;
      }

       return $code;
   }
   //生成普通代理授权码
   public function build_normal($offical,$zoon,$agent)
   {
      $data2['offical']=$offical;
      $data2['zoon']=$zoon;
      $data2['agent']=$agent;
      $code = M('relative')->distinct ( true )->order('normal desc')->where($data2)->getField ( 'normal');
      $code=$code+1;
      if($code>999){
        return false;
      }
      if(mb_strlen($code)<2){
        $code="00".$code;
      }
      else if(mb_strlen($code)<3)
      {
        $code="0".$code;
      }

       return $code;

   }
 
 //授权到期时间
   public function month_to_time($add_month)
   {		   
             //授权时间
			$endtime=time()+$add_month*3600*24*30;
			return $endtime;
   }

}